﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using System.Web.UI.WebControls;
using BackEnd.Common;
using BackEnd.Models;
using BusinessLayer.FindArtInfo;
using BusinessLayer.RegInfo;
using BusinessLayer.RegInfo.Entities;
using BusinessLayer.RegInfo.Models;
using CommandLib;
using Constants = BackEnd.Common.Constants;

namespace BackEnd.Controllers
{
    public class ReportController : Controller
    {
        //
        // GET: /Report/
        [CustomAuthorize(Action = Constants.AdminActions.ReportAccount)]
        public ActionResult Account()
        {
            ReportAccountModel model = new ReportAccountModel();
            model.ListCountry = new BusinessLayer.RegInfo.CountryBL().GetAll();
            model.ListPartner = new PartnerBL().GetAll();
            return View(model);
        }

        [CustomAuthorize(Action = Constants.AdminActions.ReportLoginLog)]
        public ActionResult LoginLog()
        {
            return View();
        }

        [CustomAuthorize(Action = Constants.AdminActions.ReportTransactionLog)]
        public ActionResult TransactionLog()
        {
            ReportTransactionLogModel model = new ReportTransactionLogModel();
            model.ListSubscription = new SubscriptionBL().GetAll();
            model.ListPartner = new PartnerBL().GetAll();
            return View(model);
        }

        public ActionResult ExportListAccountNotIntBlackList()
        {
            List<AccountExtend> listAccount = new AccountBL().GetAllAccountNotInBlackList();
            foreach (AccountExtend accountExtend in listAccount)
            {
                string key = Encrypt.EncodeTo64(Crypto.EncryptString("#39&()9TJHaq@!fg", accountExtend.Email));
                accountExtend.Key = string.Format("http://www.findartinfo.com/members/{0}",
                                                  key);
            }
            if (listAccount != null)
            {
                var gv = new GridView();
                gv.DataSource = listAccount;
                gv.DataBind();

                System.Web.HttpContext.Current.Response.ClearContent();
                System.Web.HttpContext.Current.Response.Buffer = true;
                System.Web.HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "FileName.xls"));
                System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
                System.Web.HttpContext.Current.Response.Charset = "UTF-8";
                System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
                System.Web.HttpContext.Current.Response.Charset = "";

                using (StringWriter sw = new StringWriter())
                {
                    using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                    {
                        gv.RenderControl(htw);
                        System.Web.HttpContext.Current.Response.Output.Write(sw.ToString());
                        System.Web.HttpContext.Current.Response.Flush();
                        System.Web.HttpContext.Current.Response.End();
                    }
                }

                return new EmptyResult();
            }
            return new EmptyResult();
        }
    }
}
